package com.itheima.health.controller.backend;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hm
 * @version 1.0
 * @date Created in 2024/7/13 17:13
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Resource
    private CheckGroupService checkGroupService;
    @Resource
    private SetmealService setmealService;
    /**
     * 实现体检检查组得分页查询
     */
    @PostMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.findPage(queryPageBean);
    }
    /**
     * 通过id查询checkgroup
     */
    @GetMapping("findById.do")
    public Result findById(Integer id){
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupService.findById(id)) ;
    }
    /**
     * 通过groupId查询相关联表得checkitem得id
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId.do")
    public Result findCheckItemIds(Integer checkGroupId) {
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkGroupService.findCheckItemIds(checkGroupId));
    }
    /**
     * 新增体检组
     */
    @PostMapping("/add.do")
    public Result add(@RequestBody CheckGroup checkGroup,Integer checkItemIds[]){
        checkGroupService.add(checkGroup,checkItemIds);
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    /**
     * 编辑体检组
     */
    @PostMapping("/edit.do")
    public Result update(@RequestBody CheckGroup checkGroup,Integer checkItemIds[]){
        checkGroupService.update(checkGroup, checkItemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
    /**
     * 删除体检组
     */
    @GetMapping("delete.do")
    public Result delete(Integer checkGroupId){
        List<Integer> setmeal = checkGroupService.findSetmeal(checkGroupId);
        if (setmeal.size() != 0) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        } else {
            checkGroupService.delete(checkGroupId);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    /**
     * 查询所有检查组
     */
    @GetMapping("/findAll.do")
    public Result findAll(){
        List<CheckGroup> checkGroup = checkGroupService.find();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }
}
